# USAGE
# python search.py --dataset images --shelve db.shelve --query images/84eba74d-38ae-4bf6-b8bd-79ffa1dad23a.jpg

# import the necessary packages
import shutil
import os
from PIL import Image
import imagehash
import argparse
import shelve

# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-d", "--dataset", required = True,
	help = "path to dataset of images")
ap.add_argument("-s", "--shelve", required = True,
	help = "output shelve database")
ap.add_argument("-q", "--query", required = True,
	help = "path to the query image")
args = vars(ap.parse_args())

# open the shelve database
db = shelve.open(args["shelve"])

#iterator db
# for item in db.items():
# 	print('key[{}]=value[{}]'.format(item[0], db[item[0]]))

# load the query image, compute the difference image hash, and
# and grab the images from the database that have the same hash
# value
query = Image.open(args["query"])
h = str(imagehash.dhash(query))
#make tmp dir
print 'current work dir is ' + os.getcwd()
os.mkdir(h)

print('this pic hash value is [{}]'.format(h))
filenames = db[h]

print "Found %d images" % (len(filenames))

# loop over the images
for filename in filenames:
	# image = Image.open(args["dataset"] + "/" + filename)
	# image.show()
	shutil.copy(args["dataset"] + "/" + filename, h + "/" + filename)

# close the shelve database
db.close()